package com.renwen.sms.controller;


import com.renwen.sms.dto.*;
import com.renwen.sms.entity.PingJia;
import com.renwen.sms.entity.Student;

import com.renwen.sms.exception.MyRuntimeException;
import com.renwen.sms.service.StudentService;
import com.renwen.sms.util.Result;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * student控制器
 *
 * @author zuo, shunnian
 */
@RestController
@CrossOrigin
@RequestMapping("/student/")
public class StudentController {
    @Resource
    private StudentService studentService;

    /**
     * 获取所有的学生列表
     *
     * @param page 页码
     * @param size 每页显示条数
     * @return 学生列表  select * from xx limit 5, 5;
     */
    @PostMapping("list")
    public PageResult<StudentDto> list(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return studentService.list(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException(e.getMessage());
        }
    }

    /**
     * 添加学生
     *
     * @param
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Student student) {
        try {
            studentService.save(student);
            return new Result("添加成功！", student.getSNo());
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 修改
     *
     * @param student 学生对象
     * @return 操作提示信息
     */
    @PostMapping("update")
    public Result update(@RequestBody Student student) {
        try {
            studentService.update(student);
            return new Result("修改成功！", student.getSNo());
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 根据学生id编号查询信息
     *
     * @param id 编号
     * @return 学生信息信息
     */
    @GetMapping("getById")
    public Result get(Integer id) {
        try {
            return studentService.get(id);
        } catch (Exception e) {
            throw new MyRuntimeException("系统错误，嘻嘻！");
        }
    }

    /**
     * 删除学生
     *
     * @param sNo 学号
     * @return 操作提示信息
     */
    @GetMapping("delete")
    public Result del(String sNo) {
        try {
            studentService.delete(sNo);
            return new Result("删除成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 根据学生编号组批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping("batchDel")
    public Result batchDel(String ids) {
        try {
            studentService.batchDelete(ids);
            return new Result("删除成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 导出学生表信息到桌面
     */
    @GetMapping("out")
    public Result outStudent() {
        try {
            studentService.outStudent();
            return new Result("导出成功！");
        } catch (Exception e) {
            return new Result(500, e.getMessage());
        }
    }

    /**
     * 学生查询自己能选的课程信息
     * @param page 页
     * @param size 尺寸
     * @param formMap 表格信息
     * @return course集合
     */
    @PostMapping("courseList")
    public PageResult<CourseDto> studentList(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return studentService.courseList(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 学生网上选课
     * @param cNo 当前课程课程号
     * @param sNo 当前学生学生号
     * @return 选课结果
     */
    @PostMapping("xuanke")
    public Result studentXuanKe(String cNo,String sNo){
        try {
            studentService.studentXuanKe(sNo,cNo);
            return new Result(200,"选课成功！");
        }catch (Exception e){
            return new Result(400,e.getMessage());
        }

    }

    /**
     * 查询所有成绩信息
     * @param page 页
     * @param size 尺寸
     * @param formMap 表格信息
     * @return xuanKeDto集合
     */
    @PostMapping("studentGradesList")
    public PageResult<XuanKeDto> studentGradesList(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return studentService.studentGradesList(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException(e.getMessage());
        }
    }

    /**
     * 取消选课
     *
     * @return 操作提示信息
     */
    @GetMapping("delXuanKe")
    public Result delXuanKe(String cNo,String sNo) {
        try {
            studentService.delXuanKe(cNo,sNo);
            return new Result(200,"取消成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 查询已完结课程信息:用于课程评价
     * @param page 页
     * @param size 尺寸
     * @param formMap 表格信息
     * @return 已完结课程信息集合
     */
    @PostMapping("studentJieKeList")
    public PageResult<XuanKeDto> studentJieKeList(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return studentService.studentJieKeList(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 学生对已经完结的课程进行评价
     */
    @PostMapping("pingJia")
    public Result pingJia(@RequestBody PingJia pingJia) {
        try {
            studentService.pingJia(pingJia);
            return new Result("评价成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }


}
